*Written by Emily Smith-Greenaway (smithgre@usc.edu) 
*This file reproduces estimates produced by the DHS and MICS data. 
*Note that all data are publically available at  https://dhsprogram.com/ (DHS) and https://mics.unicef.org/ (MICS)
*User agreements prohibit us from posting these data here. Before reproducing estimates, you must first gain public-use access to the data. 

************************************************************************
************************************************************************
************************************************************************
************************************************************************DHS data 
************************************************************************
************************************************************************
************************************************************************

/*

Step 1. Gain public access of DHS data: https://dhsprogram.com/data/ 

Step 2. append and keep the following variables: 
keep caseid v005 v001 v022 v201 v008 v613 v011 v000 v007 v006 v013 v012 ///
b1_01-b1_20 b2_01-b2_20 b7_01-b7_20 b8_01-b8_20 v212 bidx_01-bidx_20

Step 3. run this code: 

*/

forv i=1/20 {
recode b2_`i' 99=1999 ///
98=1998 97=1997 96=1996 95=1995 94=1994 93=1993 92=1992 91=1991 90=1990 ///
89=1989 88=1988 87=1987 86=1986 85=1985 84=1984 83=1983 82=1982 81=1981 80=1980 ///
79=1979 78=1978 77=1977 76=1976 75=1975 74=1974 73=1973 72=1972 71=1971 70=1979 ///
69=1969 68=1968 67=1967 66=1966 65=1965 64=1964 63=1963 62=1962 61=1961 60=1960 ///
59=1959 58=1958 57=1957 56=1956 55=1955 54=1954 53=1953 52=1952 51=1951 50=1950 ///
49=1949 48=1948 ///
0=.
} 

recode v007 99=1999 98=1998 97=1997 96=1996 95=1995 94=1994 93=1993 92=1992 91=1991 90=1990 ///
89=1989 88=1988 87=1987 86=1986 85=1985 84=1984 83=1983 82=1982 81=1981 80=1980 ///
79=1979 78=1978 77=1977 76=1976 75=1975 74=1974 73=1973 72=1972 71=1971 70=1979 ///
69=1969 68=1968 67=1967 66=1966 65=1965 64=1964 63=1963 62=1962 61=1961 60=1960 ///
59=1959 58=1958 57=1957 56=1956 55=1955 54=1954 53=1953 52=1952 51=1951 50=1950 ///
49=1949 48=1948 ///
0=.

forv i=1/20 {
gen mbirth_`i'= b1_`i' 
}
forv i=1/20 {
gen ybirth_`i'= b2_`i' 
}
forv i=1/20 {
gen cbirth_`i' = ym(ybirth_`i', mbirth_`i')
}
forv i=1/20 {
gen cdeath_`i' = cbirth_`i' + b7_`i'
}

egen newmom = rowmin(cbirth_1-cbirth_20) 
gen survey = ym(v007, v006)

egen firstdeath = rowmin(cdeath_1-cdeath_20)
gen timefirstdeath = firstdeath-newmom 

gen everdie = timefirstdeath 
replace everdie = 1 if everdie!=. 
recode everdie .=0

forv i=1/20 {
gen anydeath_`i'= cdeath_`i'
}

forv i=1/20 {
gen ufdeath_`i'= cdeath_`i'
}
forv i=1/20 {
replace ufdeath_`i'=. if b7_`i'>=59
}

forv i=1/20 {
gen imdeath_`i'= cdeath_`i'
}
forv i=1/20 {
replace imdeath_`i'=. if b7_`i'>=12
}

forv i=1/20 {
gen death_`i'=1 if cdeath_`i'!=. 
}

egen totalchilddeaths = rowtotal(death_1-death_20)

egen firstunderfivedeath = rowmin(ufdeath_1-ufdeath_20)
gen timefirstunderfivedeath = firstunderfivedeath-newmom

egen firstinfantdeath = rowmin(imdeath_1-imdeath_20)
gen timefirstinfantdeath = firstinfantdeath-newmom

gen everinfantdie = timefirstinfantdeath 
replace everinfantdie = 1 if everinfantdie!=. 
recode everinfantdie .=0

gen everunderfivedie = timefirstunderfivedeath 
replace everunderfivedie = 1 if everunderfivedie!=. 
recode everunderfivedie .=0

forv i=1/20 {
gen age_`i' = b8_`i'
}
egen age= rowmin(age_1-age_20)

forv i=1/20 {
gen agedeath`i' = b7_`i'
}
forv i=1/20 {
recode agedeath`i' 0/11=0 12/23=1 24/35=2 36/47=3 48/59=4  60/500=.
}
forv i=1/20 {
gen yeardeath`i' = b2_`i'+ agedeath`i'
}

/*
save smith-greenawayetalDHS.dta 
*/

************************************************************************
************************************************************************
************************************************************************
************************************************************************GENERATING ESTIMATES 
************************************************************************
************************************************************************
************************************************************************

/*
use smith-greenawayetalDHS.dta  
*/

gen svyweight = v005/1000000
gen strata = v022
replace strata = v023 if v000=="UA5"

svyset [pweight=svyweight], psu(v001) strata(strata) 

egen id_=group(v000)
 
gen neverchild=v201 if v012>=15 & v012<=19
sort v000
by v000: tab neverchild

save, smith-greenawayetalDHS.dta 

******************************** 
********************************mIM20-44; mIM45-49 (see Figure 1 & 2) 
********************************

/*
use smith-greenawayetalDHS.dta  
*/

drop if v201==0

gen momcurrentage=v012 

sort id_
gen reproage=1 if momcurrentage>=20 & momcurrentage<=44
gen olderage=1 if momcurrentage>=45 & momcurrentage<=49

svy, subpop(reproage): mean everinfantdie, over(id_) 
matrix define reproageIM = e(b)
matlist reproageIM
svmat reproageIM
forv i=1/73 {
gen infant2044_`i'=reproageIM`i'*1000
}

svy, subpop(olderage): mean everinfantdie, over(id_) 
matrix define olderageIM = e(b)
matlist olderageIM
svmat olderageIM
forv i=1/73 {
gen infant4549_`i'=olderageIM`i'*1000
}

gen mim2044w=.
forv i=1/73 {
egen val`i'=max(infant2044_`i')
replace mim2044w=val`i' if `i'==id_
}
drop val*

gen mim4549w=.
forv i=1/73 {
egen val_`i'=max(infant4549_`i')
replace mim4549w=val_`i' if `i'==id_
}

duplicates drop id_, force 
sort v000 v007
browse v000 v007 mim2044w mim4549w
  
********************************
********************************mU5M20-44; MU5M45-49 (see Figure 3 & 4) 
********************************

/*
use smith-greenawayetalDHS.dta 
*/

drop if v201==0

gen momcurrentage=v012 

sort id_
gen reproage=1 if momcurrentage>=20 & momcurrentage<=44
gen olderage=1 if momcurrentage>=45 & momcurrentage<=49


svy, subpop(reproage): mean everunderfive, over(id_) 
matrix define reproageU5M = e(b)
matlist reproageU5M
svmat reproageU5M
forv i=1/73 {
gen uf2044_`i'=reproageU5M`i'*1000
}

svy, subpop(olderage): mean everunderfive, over(id_) 
matrix define olderageU5M = e(b)
matlist olderageU5M
svmat olderageU5M
forv i=1/73 {
gen uf4549_`i'=olderageU5M`i'*1000
}

gen mu5m2044w=.
forv i=1/73 {
egen val`i'=max(uf2044_`i')
replace mu5m2044w=val`i' if `i'==id_
}
drop val*

gen mu5m4549w=.
forv i=1/73 {
egen val_`i'=max(uf4549_`i')
replace mu5m4549w=val_`i' if `i'==id_
}

duplicates drop id_, force 
sort v000 v007
browse v000 v007 mu5m2044w mu5m4549w  

********************************
********************************mU5M15-19; MU5M20-24; MU5M25-29; MU5M30-34; MU5M35-39; MU5M40-44 (see supplementary table 2) 
********************************

/*
use smith-greenawayetalDHS.dta 
*/

drop if v201==0

gen momcurrentage=v012 

sort id_
gen reproage1=1 if momcurrentage>=15 & momcurrentage<=19
gen reproage2=1 if momcurrentage>=20 & momcurrentage<=24
gen reproage3=1 if momcurrentage>=25 & momcurrentage<=29
gen reproage4=1 if momcurrentage>=30 & momcurrentage<=34
gen reproage5=1 if momcurrentage>=35 & momcurrentage<=39
gen reproage6=1 if momcurrentage>=40 & momcurrentage<=44


svy, subpop(reproage1): mean everunderfive, over(id_) 
matrix define reproage1U5M = e(b)
matlist reproage1U5M
svmat reproage1U5M
forv i=1/73 {
gen uf1519_`i'=reproage1U5M`i'*1000
}

svy, subpop(reproage2): mean everunderfive, over(id_) 
matrix define reproage2U5M = e(b)
matlist reproage2U5M
svmat reproage2U5M
forv i=1/73 {
gen uf2024_`i'=reproage2U5M`i'*1000
}

svy, subpop(reproage3): mean everunderfive, over(id_) 
matrix define reproage3U5M = e(b)
matlist reproage3U5M
svmat reproage3U5M
forv i=1/73 {
gen uf2529_`i'=reproage3U5M`i'*1000
}

svy, subpop(reproage4): mean everunderfive, over(id_) 
matrix define reproage4U5M = e(b)
matlist reproage4U5M
svmat reproage4U5M
forv i=1/73 {
gen uf3034_`i'=reproage4U5M`i'*1000
}

svy, subpop(reproage5): mean everunderfive, over(id_) 
matrix define reproage5U5M = e(b)
matlist reproage5U5M
svmat reproage5U5M
forv i=1/73 {
gen uf3539_`i'=reproage5U5M`i'*1000
}

svy, subpop(reproage6): mean everunderfive, over(id_) 
matrix define reproage6U5M = e(b)
matlist reproage6U5M
svmat reproage6U5M
forv i=1/73 {
gen uf4044_`i'=reproage6U5M`i'*1000
}

gen mu5m1519w=.
forv i=1/73 {
egen val`i'=max(uf1519_`i')
replace mu5m1519w=val`i' if `i'==id_
}
drop val*

gen mu5m2024w=.
forv i=1/73 {
egen val`i'=max(uf2024_`i')
replace mu5m2024w=val`i' if `i'==id_
}
drop val*

gen mu5m2529w=.
forv i=1/73 {
egen val`i'=max(uf2529_`i')
replace mu5m2529w=val`i' if `i'==id_
}
drop val*

gen mu5m3034w=.
forv i=1/73 {
egen val`i'=max(uf3034_`i')
replace mu5m3034w=val`i' if `i'==id_
}
drop val*

gen mu5m3539w=.
forv i=1/73 {
egen val`i'=max(uf3539_`i')
replace mu5m3539w=val`i' if `i'==id_
}
drop val*

gen mu5m4044w=.
forv i=1/73 {
egen val`i'=max(uf4044_`i')
replace mu5m4044w=val`i' if `i'==id_
}
drop val*

duplicates drop id_, force 
sort v000 v007
browse v000 v007 mu5m1519w mu5m2024w mu5m2529w mu5m3034w mu5m3539w mu5m4044w

********************************
********************************mOM4549 (see Figure 5)
********************************

/*
use smith-greenawayetalDHS.dta 
*/

drop if v201==0

gen momcurrentage=v012 

sort id_
gen olderage=1 if momcurrentage>=45 & momcurrentage<=49

svy, subpop(olderage): mean everdie, over(id_) 
matrix define olderageOM = e(b)
matlist olderageOM
svmat olderageOM
forv i=1/73 {
gen offspring4549_`i'=olderageOM`i'*1000
}
gen mom4549w=.
forv i=1/73 {
egen val_`i'=max(offspring4549_`i')
replace mom4549w=val_`i' if `i'==id_
}
drop val_*

duplicates drop id_, force 
sort v000 v007
browse v000 v007 mom4549w 

************************************************************************
************************************************************************
************************************************************************
************************************************************************MICS data 
************************************************************************
************************************************************************
************************************************************************

***************************************************************
***************************************************************
***************************************************************
***************************************************************
***************************************************************mOM4549 (see Figure 5) 
***************************************************************
***************************************************************
***************************************************************
***************************************************************

/*
Step 1. Gain public access of MICS data: https://mics.unicef.org/surveys
Step 2. download datasets, run the following code on country-specific datasets: 
*/

clear
usespss "EXAMPLE_wm.sav"
gen v000=<Gen country ID here>
gen hh1=HH1*100
gen hh2=HH2*100
gen ln=LN*100
egen caseid=concat(v000 hh1 hh2 ln)
duplicates report caseid, missing

keep caseid v000 wmweight WDOI WDOB CM8 CEB

gen v007=int((WDOI-1)/12) + 1900

/*
3. Append the datasets & run this code: 
*/

gen ageinmonths=WDOI-WDOB
gen ageinyears=ageinmonths/12
gen v012=floor(ageinyears)

recode v007 99=1999 98=1998 97=1997 96=1996 95=1995 94=1994 93=1993 92=1992 91=1991 90=1990 ///
89=1989 88=1988 87=1987 86=1986 85=1985 84=1984 83=1983 82=1982 81=1981 80=1980 ///
79=1979 78=1978 77=1977 76=1976 75=1975 74=1974 73=1973 72=1972 71=1971 70=1979 ///
69=1969 68=1968 67=1967 66=1966 65=1965 64=1964 63=1963 62=1962 61=1961 60=1960 ///
59=1959 58=1958 57=1957 56=1956 55=1955 54=1954 53=1953 52=1952 51=1951 50=1950 ///
49=1949 48=1948 ///
0=.
recode v007 9997/9999=. 

gen everdie = CM8
replace everdie=. if CEB==0 
replace everdie=. if CEB==.
recode everdie 9=.
recode everdie 2=0
sort v000
by v000: tab everdie

gen svyweight = wmweight

svyset [pweight=svyweight] 
 
sort v000
egen id_=group(v000)

gen momcurrentage=v012 

sort id_
gen olderage=1 if momcurrentage>=45 & momcurrentage<=49

svy, subpop(olderage): mean everdie, over(id_) 
matrix define olderageOM = e(b)
matlist olderageOM
svmat olderageOM
forv i=1/32 {
gen offspring4549_`i'=olderageOM`i'*1000
}
gen mom4549w=.
forv i=1/32 {
egen val_`i'=max(offspring4549_`i')
replace mom4549w=val_`i' if `i'==id_
}
drop val_*

duplicates drop id_, force 
sort v000 v007
browse v000 v007 mom4549w 
